package com.bana.sys.controller;

import com.bana.sys.code.SystemLogType;
import com.bana.sys.model.User;
import com.bana.sys.service.LogService;
import com.bana.sys.service.LoginService;
import com.bana.sys.service.UserService;
import com.bana.sys.utils.ActionResult;
import com.bana.sys.utils.TokenRequest;
import com.bana.sys.utils.Token;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/login",method = RequestMethod.POST)
public class LoginController {


    @Resource
    private UserService userService;

    @Resource
    private LogService logService;

    @Resource
    private LoginService loginService;

    /**
     * 登陆
     * @param request yhmc,dlmm
     * @return
     */
    @RequestMapping(value = "/login.do",method = RequestMethod.POST)
    public User login(@RequestBody TokenRequest<User> request){

        User user=request.getData();
        User temp=userService.validateUser(user);
        if(temp==null)
        {
            return temp;
        }
        else
        {
            temp.setToken(Token.getToken(temp));
            request.setToken(Token.getToken(temp));
            logService.insertSystemLog(request.getToken(), SystemLogType.Login,String.format("用户名称:%s",user.getYhmc()),null);
            return temp;
        }
    }

    /**
     * 验证用户
     * @param request token
     * @return
     */
    @RequestMapping(value = "/validate.do",method = RequestMethod.POST)
    public ActionResult validate(@RequestBody TokenRequest<Object> request){
        ActionResult actionResult=new ActionResult();
        actionResult.setCzjg(loginService.validateToken(request.getToken()));
        return actionResult;
    }
}
